package com.tdkj.action.backstage.website;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.tdkj.action.BaseAction;
import com.tdkj.model.user.BeanUser;
import com.tdkj.model.website.BeanProtocol;
import com.tdkj.model.website.BeanProtocolDetail;
import com.tdkj.service.website.IBeanProtocolDetailService;
public class ProtocolDetailAction  extends BaseAction{
	private Long id;
	private IBeanProtocolDetailService protocolDetailSerice;
	private List<BeanProtocolDetail> protocolDetails=new ArrayList<BeanProtocolDetail>();
	private String[] ids;
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanProtocolDetail.class);
			}
		}
		return findAll();
	}
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public IBeanProtocolDetailService getProtocolDetailSerice() {
		return protocolDetailSerice;
	}
	@Resource
	public void setProtocolDetailSerice(
			IBeanProtocolDetailService protocolDetailSerice) {
		this.protocolDetailSerice = protocolDetailSerice;
	}
	public List<BeanProtocolDetail> getProtocolDetails() {
		return protocolDetails;
	}
	public void setProtocolDetails(List<BeanProtocolDetail> protocolDetails) {
		this.protocolDetails = protocolDetails;
	}

	public String findAll(){
		protocolDetails=protocolDetailSerice.findAll(BeanProtocolDetail.class);
		if(protocolDetails.size()==0)appendObj(20);
		Criteria criteria = this.createCriteria(BeanProtocolDetail.class);
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "protocolDetailAction!findAll.action", 10);
		return "findAll";
	}

	public String deleteProtocolDetail(){
		protocolDetailSerice.delete(this.getId(),BeanProtocolDetail.class);
		return findAll();
	}
	

	private  void appendObj(int num) {
		// TODO Auto-generated method stub
		BeanProtocolDetail bean=null;
		for(int i=0;i<num;i++){
			bean=new BeanProtocolDetail();
			//模拟数据
			BeanUser u=protocolDetailSerice.findById(5, BeanUser.class);
			BeanProtocol p=protocolDetailSerice.findById(32, BeanProtocol.class);
			bean.setUser(u);
			bean.setProtocol(p);
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			protocolDetailSerice.save(bean);
		}
	}
}
